package com.chenzhou.dao;

import com.chenzhou.common.BaseDAO;
import com.chenzhou.entity.GoodsTypeEntity;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Repository
public class GoodsTypeDAOImpl extends BaseDAO<GoodsTypeEntity>{


    @Override
    protected List<GoodsTypeEntity> resultToList(ResultSet rs) throws SQLException {
        List<GoodsTypeEntity> list = new ArrayList<>();
        while(rs.next()){
            GoodsTypeEntity gt = new GoodsTypeEntity();
            gt.setId(rs.getInt("id"));
            gt.setType_code(rs.getInt("type_code"));
            gt.setType_name(rs.getString("type_name"));
            gt.setMarkup(rs.getString("markup"));
            list.add(gt);
        }
        return list;
    }

    /**
     * 查询所有的商品分类
     * @return
     */
    public List<GoodsTypeEntity> queryAllGoodsType(){
        String sql = "select * from tbl_goods_type";
        return this.execute_Query(sql,null);
    }

    public GoodsTypeEntity queryGoodsTypeEntityByID(Integer id){
        String sql = "select * from tbl_goods_type where id = ?";
        Object[] objects = new Object[]{id};
        List<GoodsTypeEntity> list = this.execute_Query(sql,objects);
        if(list.size()>0){
            return list.get(0);
        }else{
            return null;
        }
    }


    public int add_GoodsTypeEntity(GoodsTypeEntity goodsTypeEntity){
        String sql = "insert into tbl_goods_type values(null,?,?,?)";
        Object[] objects = new Object[]{
                goodsTypeEntity.getType_code(),
                goodsTypeEntity.getType_name(),
                goodsTypeEntity.getMarkup()
        };
        return this.execute_Modify(sql,objects);
    }

    public int update_GoodsTypeEntity(GoodsTypeEntity goodsTypeEntity){
        String sql = "update tbl_goods_type set type_code=?,type_name=?,markup=? where id=?";
        Object[] objects = new Object[]{
                goodsTypeEntity.getType_code(),
                goodsTypeEntity.getType_name(),
                goodsTypeEntity.getMarkup(),
                goodsTypeEntity.getId()
        };
        return this.execute_Modify(sql,objects);
    }

    public int delete_GoodsTypeEntity(Integer id){
        String sql = "delete from tbl_goods_type where id = ?";
        Object[] objects = new Object[]{id};
        return this.execute_Modify(sql,objects);
    }
}
